package org.vaadin.html5dropzone;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.vaadin.html5dropzone.widgetset.client.ui.VHtml5DropZone.DataFlawor;

import com.vaadin.terminal.PaintException;
import com.vaadin.terminal.PaintTarget;
import com.vaadin.ui.CssLayout;

/**
 * Server side component for the VHtml5DropZone widget.
 */
@SuppressWarnings("serial")
@com.vaadin.ui.ClientWidget(org.vaadin.html5dropzone.widgetset.client.ui.VHtml5DropZone.class)
public class Html5DropZone extends CssLayout {
	
	
	private Set<DataFlawor> fethedDataTypes = new HashSet<DataFlawor>();
	private DropHandler dropHandler;
	
	public Html5DropZone() {
		setWidth("200px");
		setHeight("100px");
		fethedDataTypes.add(DataFlawor.TEXT);
		fethedDataTypes.add(DataFlawor.FILE_CONTENTS_TEXT);
	}

	@Override
	public void paintContent(PaintTarget target) throws PaintException {
		super.paintContent(target);
	}
	
	@Override
	public void changeVariables(Object source, Map variables) {
		super.changeVariables(source, variables);
		
		final Object object = variables.get(DataFlawor.TEXT.toString());
		final Object fileContents = variables.get(DataFlawor.FILE_CONTENTS_TEXT.toString());
		if(dropHandler != null) {
			Transferrable transferrable = new Transferrable() {
				public Object getData(DataFlawor text) {
					if(text == DataFlawor.TEXT) {
						return object;
					} else if (text == DataFlawor.FILE_CONTENTS_TEXT) {
						return fileContents;
					}
					return null;
				}
			};
			dropHandler.receive(transferrable );
		}
		
	}
	
	public void setDropHandler(DropHandler dropHandler) {
		this.dropHandler = dropHandler;
	}

}
